package com.party.course.service.impl;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.party.course.mapper.StudentChangeInfoMapper;
import com.party.course.domain.StudentChangeInfo;
import com.party.course.service.IStudentChangeInfoService;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.party.common.utils.SecurityUtils;

/**
 * 学员课程变动Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-29
 */
@Service
public class StudentChangeInfoServiceImpl extends ServiceImpl<StudentChangeInfoMapper, StudentChangeInfo> implements IStudentChangeInfoService {
    @Autowired
    private StudentChangeInfoMapper studentChangeInfoMapper;

    /**
     * 查询学员课程变动
     *
     * @param id 学员课程变动主键
     * @return 学员课程变动
     */
    @Override
    public StudentChangeInfo view(String id) {
        return studentChangeInfoMapper.selectStudentChangeInfoById(id);
    }

    /**
     * 查询学员课程变动列表
     *
     * @param studentChangeInfo 学员课程变动
     * @return 学员课程变动
     */
    @Override
    public List<StudentChangeInfo> findList(StudentChangeInfo studentChangeInfo) {
        return studentChangeInfoMapper.selectStudentChangeInfoList(studentChangeInfo);
    }

    /**
     * 新增学员课程变动
     *
     * @param studentChangeInfo 学员课程变动
     * @return 结果
     */
    @Override
    public int add(StudentChangeInfo studentChangeInfo) {
        if (!"0".equals(studentChangeInfo.getChangeTimeNum())){
            //非 开课变动信息  则计算变动学时
            StudentChangeInfo changeInfo = studentChangeInfoMapper.findByStuIdandOderCode(studentChangeInfo.getStuId(),studentChangeInfo.getOrderCode());
            if (ObjectUtil.isNotEmpty(changeInfo)){
                String afterTimeNum = changeInfo.getAfterTimeNum();
                if(StrUtil.isEmpty(changeInfo.getAfterTimeNum())) {
                    afterTimeNum = "0";
                }
                studentChangeInfo.setBeforTimeNum(afterTimeNum);
                int bTimeNum = 0;
                if ("0".equals(studentChangeInfo.getTyep())){
                    //出
                    bTimeNum = Integer.parseInt(afterTimeNum) - Integer.parseInt(studentChangeInfo.getChangeTimeNum());
                }else {
                    //入
                    bTimeNum = Integer.parseInt(afterTimeNum) + Integer.parseInt(studentChangeInfo.getChangeTimeNum());
                }
                studentChangeInfo.setAfterTimeNum(String.valueOf(bTimeNum));
            }
        }
        studentChangeInfo.setId(IdUtil.simpleUUID());
        studentChangeInfo.setDelFlag("0");
        return studentChangeInfoMapper.insert(studentChangeInfo);
    }

    /**
     * 修改学员课程变动
     *
     * @param studentChangeInfo 学员课程变动
     * @return 结果
     */
    @Override
    public int updateEntity(StudentChangeInfo studentChangeInfo) {
        studentChangeInfo.setUpdateTime(DateUtil.date());
        return studentChangeInfoMapper.updateById(studentChangeInfo);
    }

    /**
     * 批量删除学员课程变动
     *
     * @param ids 需要删除的学员课程变动主键
     * @return 结果
     */
    @Override
    public int deleteIds(List<String> ids) {
        return studentChangeInfoMapper.deleteStudentChangeInfoByIds(ids);
    }

    /**
     * 删除学员课程变动信息
     *
     * @param id 学员课程变动主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return studentChangeInfoMapper.deleteStudentChangeInfoById(id);
    }
}
